/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-08-06
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysEmployee;
import com.rzico.core.entity.SysOffice;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.order.entity.Askfor;
import com.rzico.order.entity.GiftCard;
import com.rzico.order.service.GiftCardService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 礼品卡控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-08-06
 */
@Api(description = "礼品卡接口")
@RestController
@RequestMapping("/admin/giftCard")
public class GiftCardAdminController extends BaseController {

    @Autowired
    private GiftCardService giftCardService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询礼品卡
     *
     * @return
    */
    @ApiOperation("分页查询礼品卡")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "receiverId", value = "领取人ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "status", value = "状态类型（0.待领取，1.待使用，2.已使用）", dataType = "String", paramType = "query")
    })
    public CommResult<GiftCard> list(Date startDate, Date endDate,Long memberId,Long receiverId,Long productId,Long status, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (memberId!=null){
            params.put("memberId", memberId);
        }
        if (receiverId!=null){
            params.put("receiverId", receiverId);
        }
        if (productId!=null){
            params.put("productId", productId);
        }
        if (status!=null){
            params.put("status", status);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<GiftCard> list = giftCardService.selectList(params);
        PageResult<GiftCard> pageResult = new PageResult<GiftCard>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条礼品卡
     *
     * @return
     */
    @ApiOperation("查询单条礼品卡")
    @GetMapping("/find/{id}")
    public CommResult<Askfor> find(@PathVariable String id) {
        GiftCard result = giftCardService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("giftCard",result);

        return CommResult.success(data);

    }

    /**
     * 更新礼品卡
     *
     * @param giftCard
     * @return
     */
    @Log(desc = "更新礼品卡", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新礼品卡")
    @PostMapping("/update")
    public CommResult<GiftCard> update(@RequestBody GiftCard giftCard) {
        int affectCount = giftCardService.updateByPrimaryKeySelective(giftCard);
        if (affectCount <= 0){
            return CommResult.error();
        }
        GiftCard result = giftCardService.findById(giftCard.getId());
        return CommResult.success(result);
    }

}
